var express = require('express');
var router = express.Router();

var reqUtil = require('../lib/req');
var LINK = require('../api/link');

router.get('/', function(req, res) {
    res.send('respond with a resource');
});

router.get('/course', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        var page = parseInt(req.query.page) || 1;
        reqUtil.getData(LINK.REQ_GET_USER_COURSE + page, function(code, data) {
            if (!code) {
                res.render('user/course', {
                    user: user,
                    list: data.page_data,
                    "page_now": data.page_now,
                    "total_page": data.total_page,
                    "total_rows": data.total_rows,
                    url: req.baseUrl + req.path
                });
            } else {
                res.redirect('/');
            }
        });
    } else {
        res.render('login', { link: req.originalUrl });
    }
});

router.get('/calendar', function(req, res) {
    // var list = [{ title: '交互设计流程解析-百度魔图案例', teacher: '王老师', startTime: '09-11 13:00', lastTime: '00:45:00', id: '1', course: { id: '1', title: 'C++程序设计', type: '1', img: '/images/310.png' } },
    //     { title: '交互设计流程解析-百度魔图案例', teacher: '王老师', startTime: '09-11 13:00', lastTime: '00:45:00', id: '2', course: { id: '1', title: 'C++程序设计', type: '2', img: '/images/310.png' } }
    // ]
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        reqUtil.getData(LINK.REQ_GET_USER_CALENDAR, function(code, data) {
            if (!code) {
                res.render('user/calendar', {
                    user: user,
                    list: data
                });
            } else {
                res.redirect('/');
            }
        });
    } else {
        res.render('login', { link: req.originalUrl });
    }

});

router.get('/note', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }

    var page = parseInt(req.query.page) || 1;
    var list = [
        { id: '1', content: '要点', cid: '1', course: '扣丁学堂_威哥_Java核心技术（全套', chapter: '扣丁学堂 JavaWeb 03 Servlet 7 Servlet商品管理案例 4', month: '09', date: '18', hour: '13:35' },
        { id: '2', content: '要点', cid: '2', course: '扣丁学堂_威哥_Java核心技术（全套', chapter: '扣丁学堂 JavaWeb 03 Servlet 7 Servlet商品管理案例 4', month: '09', date: '18', hour: '13:35' },
        { id: '3', content: '要点', cid: '3', course: '扣丁学堂_威哥_Java核心技术（全套', chapter: '扣丁学堂 JavaWeb 03 Servlet 7 Servlet商品管理案例 4', month: '09', date: '18', hour: '13:35' }
    ]
    var obj = {
        list: list,
        "page_now": page,
        "total_page": 5,
        "total_rows": 100,
        url: req.baseUrl + req.path
    };
    res.render('user/note', obj);
});

router.get('/question', function(req, res) {
    var page = parseInt(req.query.page) || 1;
    var type = parseInt(req.query.type) || 1;
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }


    var list = [
        { id: '1', content: '要点', name: 'jinxc', time: '2016-03-31 09:53:16' },
        { id: '2', content: '要点', name: 'jinxc', time: '2016-03-31 09:53:16' },
        { id: '3', content: '要点', name: 'jinxc', time: '2016-03-31 09:53:16' },
    ];
    var obj = {
        list: [{
            question: { id: '1', content: '怎么做个好的透视，谢谢', course: 'Excel2013 如何突破学习瓶颈专题', time: '2016-03-31 09:53:16' },
            answer: list
        }, {
            question: { id: '1', content: '怎么做个好的透视，谢谢', course: 'Excel2013 如何突破学习瓶颈专题', time: '2016-03-31 09:53:16' },
            answer: list
        }],
        "page_now": page,
        "total_page": 5,
        "total_rows": 100,
        url: req.baseUrl + req.path,
        type: type
    };
    res.render('user/question', obj);
});

router.get('/order', function(req, res) {


    // var list = [
    //     { id: '1', num: '123456', state: '1', price: '100', time: '2016-04-26 11:24:23', course: { id: '1', title: '走进电商走近淘宝', img: '/images/310.png', teacher: '教师名字（取前2位）', type: '1' } },
    //     { id: '1', num: '123456', state: '2', price: '100', time: '2016-04-26 11:24:23', course: { id: '1', title: '走进电商走近淘宝', img: '/images/310.png', teacher: '教师名字（取前2位）', type: '2' } },
    // ]
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        var page = parseInt(req.query.page) || 1;
        var state = parseInt(req.query.state);
        if (!state) {
            state = 0;
        }
        reqUtil.getData(LINK.REQ_GET_USER_ORDER, function(code, data) {
            if (!code) {
                res.render('user/order', {
                    user: user,
                    list: data.page_data,
                    "page_now": data.page_now,
                    "total_page": data.total_page,
                    "total_rows": data.total_rows,
                    url: req.baseUrl + req.path,
                    state: state
                });
            } else {
                res.redirect('/');
            }
        });
    } else {
        res.render('login', { link: req.originalUrl });
    }

});

router.get('/apply', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }

    var page = parseInt(req.query.page) || 1;
    var state = parseInt(req.query.state);
    if (!state) {
        state = 0;
    }
    var list = [
        { id: '1', orderId: '1', num: '123456', state: '1', price: '100', time: '2016-04-26 11:24:23', course: { id: '1', title: '走进电商走近淘宝', img: '/images/310.png', teacher: '教师名字（取前2位）', type: '1' } },
        { id: '2', orderId: '2', num: '123456', state: '2', price: '100', time: '2016-04-26 11:24:23', course: { id: '1', title: '走进电商走近淘宝', img: '/images/310.png', teacher: '教师名字（取前2位）', type: '2' } },
    ]
    var obj = {
        list: list,
        "page_now": page,
        "total_page": 5,
        "total_rows": 100,
        url: req.baseUrl + req.path,
        state: state
    };
    res.render('user/apply', obj);
});

router.get('/cart', function(req, res) {
    // var clist = [
    //     { type: 1, startTime: '2016-09-01', title: 'C++程序设计', price: 200, school: '嘉兴智慧教育', id: '1', img: '/images/310.png', info: '课程简述', times: '10' },
    //     { type: 2, startTime: '2016-09-01', title: 'JAVA程序设计', price: 100, school: '嘉兴智慧教育', id: '2', img: '/images/310.png', info: '课程简述', times: '10' },
    //     { type: 1, startTime: '2016-09-01', title: 'C++程序设计', price: 50, school: '嘉兴智慧教育', id: '3', img: '/images/310.png', info: '课程简述', times: '10' }
    // ];
    // var obj = {
    //     list: clist,
    // };
    // res.render('user/cart', obj);
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        reqUtil.getData(LINK.REQ_GET_USER_CART, function(code, data) {
            if (!code) {
                res.render('user/cart', {
                    user: user,
                    list: data
                });
            } else {
                res.redirect('/');
            }
        });
    } else {
        res.render('login', { link: req.originalUrl });
    }

});


router.post('/note/del', function(req, res) {

    var obj = {};
    console.log(req.body);
    res.json({
        code: 0,
        result: ''
    });
});

router.post('/note/edit', function(req, res) {

    var obj = {};
    console.log(req.body);
    res.json({
        code: 0,
        result: ''
    });
});

router.get('/cart/add', function(req, res) {
    var obj = {};
    console.log(req.body);
    res.json({
        code: 0,
        num: '1'
    });

});

router.post('/cart/del', function(req, res) {
    var obj = {};
    console.log(req.body);
    res.json({
        code: 0,
    });

});

module.exports = router;